package cn.suimg.neo4j.repository;

import cn.suimg.neo4j.graph.AlbumGraph;
import cn.suimg.neo4j.graph.SingerGraph;
import cn.suimg.neo4j.graph.SongGraph;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SongGraphRepository extends Neo4jRepository<SongGraph,Long> {

    /**
     * 根据歌曲ID查询
     * @param id
     * @return
     */
    SongGraph findBySongId(@Param("songId") Integer songId);

    @Query(value = "match (singer:歌手) -[:演唱] -> (songGraph:歌曲) where singer.singerId = {singerId} return songGraph",
            countQuery = "match (singer:歌手) -[:演唱] -> (songGraph:歌曲) where singer.singerId = {singerId} return distinct count(songGraph)")
    Page<SongGraph> getBySingerId(@Param("singerId") Integer singerId, Pageable page);



    @Query(value = "match (album:专辑) -[:包含] -> (songGraph:歌曲) where album.albumId = {albumId} return songGraph",
            countQuery = "match (album:专辑) -[:包含] -> (songGraph:歌曲) where album.albumId = {albumId} return distinct count(songGraph)")
    Page<SongGraph> getByAlbumId(@Param("albumId") Integer albumId, Pageable page);

    @Query("match (songGraph:歌曲 {songId:{songId}}) <- [:演唱] - (singerGraph:歌手) return singerGraph")
    List<SingerGraph> getSingedSingerBySongId(@Param("songId") Integer songId);

    @Query("match (songGraph:歌曲 {songId:{songId}}) <- [:包含] - (albumGraph:专辑) return albumGraph")
    List<AlbumGraph> getIncludeSongAlbumList(@Param("songId") Integer songId);

    @Query("match (album:专辑{albumId:{albumId}}) -[:包含] -> (songGraph:歌曲)  return songGraph")
    List<SongGraph> findByAlbumId(@Param("albumId") Integer albumId);

    @Query("match (singer:歌手{singerId:{singerId}}) -[:演唱] -> (songGraph:歌曲) return songGraph")
    List<SongGraph> findBySingerId(@Param("singerId") Integer singerId);

}
